package com.cloudmusic.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cloudmusic.constant.Const;
import com.cloudmusic.entity.Music;
import com.cloudmusic.entity.User;
import com.cloudmusic.service.AddToSongListService;

@Controller
@RequestMapping("music/addtosonglist")
public class AddToSongListController {
	
	@Autowired
	AddToSongListService songListService;

	private Logger log=Logger.getLogger(this.getClass());
	
	//多曲添加到我喜欢（功能菜单）
	@RequestMapping("addtolikes")
	@ResponseBody
	public JSONObject addToLikesSongList(HttpSession session,@RequestBody List<Music> listSelected) {
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.addToLikesSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),listSelected);
			jsonObject.put("Tip",true);
			log.info("多曲添加到我喜欢成功");
		} catch (Exception e) {
			log.error("多曲添加到我喜欢失败", e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
	//多曲添加到我的收藏（功能菜单）
	@RequestMapping("addtocollects")
	@ResponseBody
	public JSONObject addToCollectsSongList(HttpSession session,@RequestBody List<Music> listSelected) {
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.addToCollectsSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),listSelected);
			jsonObject.put("Tip",true);
			log.info("多曲添加到的收藏成功");
		} catch (Exception e) {
			log.error("多曲添加到的收藏失败",e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
	//多曲添加到自定义歌单（功能菜单）
	@RequestMapping("addtosheets")
	@ResponseBody
	public JSONObject addToSheetsSongList(HttpSession session,String songListName,String listSelected) {
		String string[] = StringUtils.strip(listSelected, "[]").split(",");
		for(int i=0;i<string.length;i++) {
			string[i]=StringUtils.strip(string[i],"\"\"");
		}
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.addToSheetsSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),songListName,string);
			jsonObject.put("Tip",true);
			log.info("多曲添加到"+songListName+"成功");
		} catch (Exception e) {
			log.error("多曲添加到"+songListName+"失败",e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
	//多曲删除（功能菜单）
	@RequestMapping("outtosheets")
	@ResponseBody
	public JSONObject outToSheetsSongList(HttpSession session,String songListName,String listSelected) {
		JSONObject jsonObject = new JSONObject();
		String string[] = StringUtils.strip(listSelected, "[]").split(",");
		for(int i=0;i<string.length;i++) {
			string[i]=StringUtils.strip(string[i],"\"\"");
		}
		try {
			songListService.outToSheetsSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(), songListName.trim(), string);
			jsonObject.put("Tip",true);
			log.info("多曲删除成功");
		} catch (Exception e) {
			log.error("多曲删除失败",e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}

	//单曲添加到我喜欢（小爱心）
	@RequestMapping("addtolike")
	@ResponseBody
	public JSONObject addToLikeSongList(HttpSession session,@RequestParam(value = "mid") String mid) {
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.addToLikeSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),mid);
			jsonObject.put("Tip",true);
			log.info("单曲添加到我喜欢成功");
		} catch (Exception e) {
			log.error("单曲添加到我喜欢失败",e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
	//单曲删除我喜欢（小爱心）
	@RequestMapping("outtolike")
	@ResponseBody
	public JSONObject outToLikeSongList(HttpSession session,@RequestParam(value = "mid") String mid) {
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.outToLikeSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),mid);
			jsonObject.put("Tip",true);
			log.info("单曲删除我喜欢成功");
		} catch (Exception e) {
			log.error("单曲删除我喜欢失败",e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
	//单曲添加到我的收藏（小圆圈）
	@RequestMapping("addtocollect")
	@ResponseBody
	public JSONObject addToCollectSongList(HttpSession session,@RequestParam(value = "mid") String mid) {
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.addToCollectSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),mid);
			jsonObject.put("Tip",true);
			log.info("单曲添加到我的收藏成功");
		} catch (Exception e) {
			log.error("单曲添加到我的收藏失败",e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
	//单曲添加到自定义歌单（小圆圈）
	@RequestMapping("addtosheet")
	@ResponseBody
	public JSONObject addToSheetSongList(HttpSession session,@RequestParam(value = "songListName") String songListName,@RequestParam(value = "mid") String mid) {
		JSONObject jsonObject = new JSONObject();
		try {
			songListService.addToSheetSongList(((User)session.getAttribute(Const.SESSION_KEY)).getUid(),songListName,mid);
			jsonObject.put("Tip",true);
			log.info("单曲添加到"+songListName+"成功");
		} catch (Exception e) {
			log.error("单曲添加到"+songListName+"失败", e);
			jsonObject.put("Tip",false);
		}
		return jsonObject;
	}
	
}
